package main.model;

import java.util.List;
import java.io.Serializable;

import javax.persistence.*;

import main.util.HibernateContext;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

@Entity
@Table(name="Airline")
public class Airline implements Serializable 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4489876102974476811L;
	
	private int airlineID;
	private int flightNumber;
	private int originAirportID;
	private String origin;
	private String destination;
	private Airport airport;

	
	public Airline() {}
	
	public Airline(int airlineID, int flightNumber, int originAirportID, 
			String origin, String destination) 
	{
		this.airlineID = airlineID;
		this.flightNumber = flightNumber;
		this.originAirportID = originAirportID;
		this.origin = origin;
		this.destination = destination; 
	}
	
	public Airline(int airlineID, int flightNumber, Airport airport, 
				String origin, String destination)
	{
		this.airlineID = airlineID;
		this.flightNumber = flightNumber;
		this.airport = airport;
		this.origin = origin;
		this.destination = destination; 
	}
	
	
	@Id
	@GeneratedValue
	@Column(name="AIRLINE_ID")
	public int getDescId() {
		return airlineID;
	}
	public void setDescId(int airlineID) {
		this.airlineID = airlineID;
	}
	
	@Column(name="flightNumber")
	public int getFlightNumber() {
		return flightNumber;
	}
	public void setFlightNumber(int flightNumber) {
		this.flightNumber = flightNumber;
	}
	
	@Column(name="ORIGIN")
	public String getOrigin() {
		return origin;
	}
	public void setOrigin(String origin) {
		this.origin = origin;
	}
	
	@Column(name="DESTINATION")
	public String getDestination() {
		return destination;
	}
	public void setDestination(String destination) {
		this.destination = destination;
	}
	
	@ManyToOne (cascade = CascadeType.ALL)
	@JoinColumn (name = "ORIGIN_AIRPORT_ID")
	public Airport getAirport() 
	{ return airport; }
	public void setAirport(Airport airport)
	{ this.airport = airport; }
	
//	@Column(name="ORIGIN_AIRPORT_ID")
//	public int getOriginAirportID() {
//		return originAirportID;
//	}
//	public void setOrigin(int originAirportID) {
//		this.originAirportID = originAirportID;
//	}
	/**
	 *load the airport description data into table
	 */
	public static void load() {
		Session session = HibernateContext.getSession();
		
		Airport lvi = new Airport("Lehigh Valley International");
		
        Transaction tx = session.beginTransaction();
        {
			session.save(new Airline(20366,4877,lvi,"ABE","DTW"));
			session.save(new Airline(20366,4891,lvi,"ABE","ATL"));
			session.save(new Airline(20366,5009,lvi,"ABE","DTW"));
			session.save(new Airline(20366,5121,lvi,"ABE","DTW"));
			session.save(new Airline(20366,5209,lvi,"ABE","DTW"));
			session.save(new Airline(20366,5221,lvi,"ABE","ATL"));
			session.save(new Airline(20366,5579,lvi,"ABE","ATL"));
			session.save(new Airline(20366,5860,lvi,"ABE","ORD"));
		}
        tx.commit();
        session.close();
		
		System.out.println("AirLine Data Loaded");
		
	}
	
	/**
	 * retrieve the airport description by criteria
	 */
	public static List<Airline> retrieveAll() {
		Session session = HibernateContext.getSession();
		Criteria criteria = session.createCriteria(Airline.class);
		List<Airline> result = (List<Airline>) criteria.list();
		
		session.close();
		return result;
	}
	
	public void print()
	{
		System.out.printf("%d:\t%d\t\t%s\t%s\t\t%s\n", airlineID, flightNumber, 
				origin, destination, airport.getName());
	}
	
	public void printInSession()
	{
		Session session = HibernateContext.getSession();
		session.update(this);
		print();
		session.close();
	}
}
